import Navigator from "../../components/navigator/Navigators"
import { View, Button, Text } from '@tarojs/components/'
import { set_address } from "../../actions/positioning";
import QQMapWX from "../../utils/qqmap-wx-jssdk.min.js";

import Taro from "@tarojs/taro";
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";

import './index.scss'

const Home = (props) => {
  const dispatch = useDispatch();
  const [jump, setJump] = useState({
    title: "跳转目标",
    url: ""
  });

  const { } = useSelector(s => s);

  // 实例化 腾讯地图
  const qqmapsdk = new QQMapWX({
    key: "4JJBZ-Y5WKU-SB2VY-BJURA-KME77-LXBBE",
  });
  // 申请定位方法
  const positioning = () => {
    Taro.getSetting({
      success: function (res) {
        if (res.authSetting["scope.userLocation"] === true) {
          // 已经授权，非第一次
          Taro.getLocation({
            type: "wgs84",
            success(locationRes) {
              qqmapsdk.reverseGeocoder({
                location: {
                  latitude: locationRes.latitude,
                  longitude: locationRes.longitude,
                },
                success: function (result) {
                  // 设置地址
                  dispatch(set_address({
                    latitude: locationRes.latitude,
                    longitude: locationRes.longitude,
                    address: result.result.address,
                  }));
                },
              });
            },
          });
        } else if (res.authSetting["scope.userLocation"] === undefined) {
          // 第一次登陆且未授权
          Taro.getLocation({
            type: "wgs84",
            success(locationRes) {
              dispatch(set_address({
                latitude: locationRes.latitude,
                longitude: locationRes.longitude,
                address: "上海八维宏烨教育",
              }));
            },
            fail: function () {
              Taro.showToast({
                title: "拒绝授权",
                icon: "none",
                duration: 1000,
              });
            },
          });
        } else if (
          // res.authSetting["scope.userLocation"] !== undefined &&
          // res.authSetting["scope.userLocation"] !== true
          res.authSetting["scope.userLocation"] === false
        ) {
          // 不是第一次进入该页面,且未授权
          Taro.showModal({
            title: "请求授权当前位置",
            content: "需要获取您的地理位置，请确认授权",
            success: function (res) {
              if (res.cancel) {
                // 取消授权
                Taro.showToast({
                  title: "拒绝授权",
                  icon: "none",
                  duration: 1300,
                });
              } else {
                Taro.openSetting({
                  // 授权获取地理位置
                  success: function (dataAu) {
                    // 同意
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      Taro.showToast({
                        title: "授权成功",
                        icon: "success",
                        duration: 1000,
                      });
                      Taro.chooseLocation({
                        // 选择地理位置
                        success: function (res) {
                          dispatch(set_address({
                            latitude: res.latitude,
                            longitude: res.longitude,
                            address: res.address,
                            name: res.name,
                          }));
                          Taro.showToast({
                            title: "获取成功",
                            icon: "success",
                            duration: 1000,
                          });
                        },
                        fail: function () {
                          // 定位失败
                          Taro.showToast({
                            title: "定位失败",
                            icon: "none",
                            duration: 1300,
                          });
                        },
                      });
                    } else {
                      // 不同意
                      Taro.showToast({
                        title: "授权失败",
                        icon: "none",
                        duration: 1000,
                      });
                    }
                  },
                });
              }
            },
          });
        }
      },
      fail: function (err) {
        console.log(err, "-----err");
      },
    });
  };

  useEffect(() => {
    // 进入页面自动申请定位
    positioning();
  }, []);


  return (
    <View className='content'>
      <Navigator jump={jump}></Navigator>
      <View className="main"></View>
    </View>
  )
};


export default Home


